package com.syn.vr.vm;

import android.app.Application;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.gaoyi.basemodule.apiresult.ApiResultData;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.SPUtil;
import com.gaoyi.basemodule.baseUtil.ToastUtil;
import com.gaoyi.basemodule.baseconstant.SPName;
import com.gaoyi.basemodule.basevm.BaseViewModel;
import com.google.gson.reflect.TypeToken;
import com.syn.vr.R;
import com.syn.vr.activity.MainActivity;
import com.syn.vr.bean.LoginDto;
import com.syn.vr.bean.ShortDto;
import com.syn.vr.bean.UserDto;
import com.syn.vr.bean.query.LoginQuery;
import com.syn.vr.bean.query.ResetPwdQuery;
import com.syn.vr.constant.ApiStatus;
import com.syn.vr.constant.Constants;
import com.syn.vr.repository.UserRepository;
import com.syn.vr.util.RSAUtil;

import java.util.Map;

public class LoginViewModel extends BaseViewModel {

    MutableLiveData<String> mAccount;
    MutableLiveData<String> mPwd;

    MutableLiveData<String> mEmail;
    MutableLiveData<String> mNewPwd;
    MutableLiveData<String> mConfirmPwd;

    MutableLiveData<Integer> mForgotPwdCode;
    MutableLiveData<Integer> mResetPwdCode;

    final UserRepository mRepository;

    public LoginViewModel(@NonNull Application application) {
        super(application);
        mRepository = new UserRepository();
    }

    public MutableLiveData<Integer> getForgotPwdCode() {
        if (mForgotPwdCode == null) {
            mForgotPwdCode = new MutableLiveData<>();
        }
        return mForgotPwdCode;
    }

    public void setForgotPwdCode(Integer value) {
        mForgotPwdCode.setValue(value);
    }

    public MutableLiveData<Integer> getResetPwdCode() {
        if (mResetPwdCode == null) {
            mResetPwdCode = new MutableLiveData<>();
        }
        return mResetPwdCode;
    }

    public void setResetPwdCode(Integer value) {
        mResetPwdCode.setValue(value);
    }

    public MutableLiveData<String> getAccount() {
        if (mAccount == null) {
            mAccount = new MutableLiveData<>();
        }
        return mAccount;
    }

    public void setAccount(String account) {
        mAccount = new MutableLiveData<>();
        mAccount.setValue(account);
    }

    public MutableLiveData<String> getPwd() {
        if (mPwd == null) {
            mPwd = new MutableLiveData<>();
        }
        return mPwd;
    }

    public void setPwd(String pwd) {
        mPwd = new MutableLiveData<>();
        mPwd.setValue(pwd);
    }

    public MutableLiveData<String> getEmail() {
        if (mEmail == null) {
            mEmail = new MutableLiveData<>();
        }
        return mEmail;
    }

    public void setEmail(String value) {
        mEmail.setValue(value);
    }

    public MutableLiveData<String> getNewPwd() {
        if (mNewPwd == null) {
            mNewPwd = new MutableLiveData<>();
        }
        return mNewPwd;
    }

    public void setNewPwd(String value) {
        mNewPwd.setValue(value);
    }

    public MutableLiveData<String> getConfirmPwd() {
        if (mConfirmPwd == null) {
            mConfirmPwd = new MutableLiveData<>();
        }
        return mConfirmPwd;
    }

    public void setConfirmPwd(String value) {
        mConfirmPwd.setValue(value);
    }

    public void login(boolean isRemPwd) {
        LoginQuery bean = new LoginQuery();
        bean.setUserName(mAccount.getValue());
        bean.setPassword(mPwd.getValue());
        mRepository.login(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                ApiResultData<LoginDto> apiResultData = (ApiResultData<LoginDto>) apiResult;
                LoginDto data = apiResultData.getData();
                mResultCode.setValue(apiResultData.getStatus());
                ToastUtil.showToast(apiResultData.getMessage());
                if (apiResultData.getStatus() == ApiStatus.SUCCESS) {
                    UserDto userInfo = data.getUserInfo();
                    if (isRemPwd) {
                        Map<Integer, String> key = RSAUtil.getKey();
                        if (!key.isEmpty()) {
                            String encipherPwd = RSAUtil.encipher(mPwd.getValue(), key.get(0));
                            encipherPwd += Constants.SPLIT + key.get(1);
                            SPUtil.getInstance().putString(SPName.PASSWORD, encipherPwd);
                        } else {
                            SPUtil.getInstance().putString(SPName.PASSWORD, "");
                        }
                    } else {
                        SPUtil.getInstance().putString(SPName.PASSWORD, "");
                    }
                    SPUtil.getInstance().putString(SPName.ACCOUNT, userInfo.getUserName());
                    SPUtil.getInstance().putString(SPName.ID, userInfo.getId());
                    SPUtil.getInstance().putString(SPName.TOKEN,
                            data.getToken() == null ? "" : data.getToken());
                    SPUtil.getInstance().putString(SPName.REFRESH_TOKEN,
                            data.getRefreshToken() == null ? "" : data.getRefreshToken());
                    ShortDto site = userInfo.getProj();
                    if (site == null) {
                        SPUtil.getInstance().putString(SPName.PROJ_ID, "");
                        SPUtil.getInstance().putString(SPName.PROJ_NAME, "");
                    } else {
                        SPUtil.getInstance().putString(SPName.PROJ_ID, site.getId());
                        SPUtil.getInstance().putString(SPName.PROJ_NAME, site.getName());
                    }
                    Intent intent = new Intent(mApplication, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    mApplication.startActivity(intent);
                }
            }

            @Override
            public void onFails(Exception e) {
                mResultCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<LoginDto>>() {
        }.getType());
    }

    public void forgotPwd() {
        mRepository.forgotPwd(mEmail.getValue(), new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                ApiResultData<String> apiResultData = (ApiResultData<String>) apiResult;
                mForgotPwdCode.setValue(apiResultData.getStatus());
                if (apiResultData.getStatus() == ApiStatus.SUCCESS) {
                    ToastUtil.showToast(R.string.send_success);
                } else {
                    ToastUtil.showToast(apiResultData.getMessage());
                }
            }

            @Override
            public void onFails(Exception e) {
                mForgotPwdCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }

    public void resetPwd() {
        ResetPwdQuery bean = new ResetPwdQuery();
        bean.setEmail(mEmail.getValue());
        bean.setNewPassWord(mNewPwd.getValue());
        bean.setConfirmPassword(mConfirmPwd.getValue());
        mRepository.resetPwd(bean, new OnResultListener() {
            @Override
            public void onSuccess(Object apiResult) {
                ApiResultData<String> apiResultData = (ApiResultData<String>) apiResult;
                mResetPwdCode.setValue(apiResultData.getStatus());
                ToastUtil.showToast(apiResultData.getMessage());
            }

            @Override
            public void onFails(Exception e) {
                mResetPwdCode.setValue(ApiStatus.NET_ERROR);
                ToastUtil.showToast(R.string.network_error);
            }
        }, new TypeToken<ApiResultData<String>>() {
        }.getType());
    }
}
